# -*- coding: utf-8 -*-
'''eof
name:申请人当前未结清逾期(透支)总额
code:IC18_IndCurrUnsettledOvduAmt
tableName:
columnName:
groups:行内二代个人征信数据
dependencies:IC_DSJ_2D
type:常用指标
datasourceType:在线指标
description:当前信贷逾期总额
eof'''


null_type_list = ['', ' ', None, 'null', 'Null', 'NULL', '/', '-', '--']

def getTable(data, tableName, occurTimes, iteratorTimes):
    '''
    获取表信息
    :param data:表上层的字典对象
    :param tableName:表的名字
    :param occurTimes:表出现的次数，如果是0代表此表可以不出现，如果是1代表此表必须出现
    :param iteratorTimes:下级字段出现的次数，如果是0代表下级字段可以不出现，如果是1代表下级字段必须出现
    :return:该表全部数据  返回[{}]为异常缺失，返回0为正常缺失，其他返回正常数组
    '''
    try:
        table = data.get(tableName)
        if occurTimes == 0:
            if table is None or (table == "" and iteratorTimes == 0):
                return 0
            elif isinstance(table, list) is False or len(table) == 0 or (table == "" and iteratorTimes == 1):
                return [{}]
            else:
                return table
        if occurTimes == 1:
            if table == "" and iteratorTimes == 0:
                return 0
            if table is None or isinstance(table, list) is False or len(table) == 0 \
                    or (table == "" and iteratorTimes == 1):
                return [{}]
            else:
                return table
    except:
        return [{}]
    
def isFloat(x):
    '''
    判断x为float类型且大于等于0
    :param x:
    :return:
    '''
    try:
        if isinstance(float(x), float) and float(x) >= 0:
            if str(x) in ['inf', 'infinity', 'INF', 'INFINITY', 'True']:
                return False
            else:
                return True
        else:
            return False
    except:
        return False

def IC18_IndCurrUnsettledOvduAmt():
    data = IC_DSJ_2D.get('data')
    # 借贷账户信息 ,出现次数[1..1],下级字段出现次数[0..*]
    PDA = getTable(data, 'PDA', 1, 0)
    if PDA == [{}]:
        return u'缺失值'
    elif PDA == 0:
        return 0
    else:
        #借贷账户信息单元 ,出现次数[0..*],下级字段出现次数[1..1]
        PD01 = getTable(PDA[0], 'PD01', 0, 1)
        if PD01 == [{}]:
            return u'缺失值'
        elif PD01 == 0:
            return 0
        else:
            cnts = 0
            Balance_list = []
            for i in PD01:
                try:
                    #最近一次月度表现信息段,出现次数[0..1],下级字段出现次数[0..1]
                    PD01C = getTable(i, 'PD01C', 0, 0)
                    if PD01C == [{}]:
                        cnts += 1
                    elif PD01C == 0:
                        pass
                    else:
                        PD01CJ06 = PD01C[0].get("PD01CJ06")
                        if PD01CJ06 not in null_type_list and isFloat(PD01CJ06):
                            Balance_list.append(float(PD01CJ06))
                        else:
                            cnts += 1
                except:
                    cnts += 1
            if cnts == len(PD01):
                return u'缺失值'
            return sum(Balance_list)
result = IC18_IndCurrUnsettledOvduAmt()
